<?php
namespace soa\partner\gt;

class BaseInfomation extends GtClient
{
    const WAYBILL_URL = 'http://101.95.114.210:8030/WaybillNumberApi/WaybillNumber/v1/';

    const BASE_INFO_URl = 'http://101.227.81.105:8086/ScanInterceptApiTest/';//测试地址

    const BASE_INFO_KEY = '5A37EB066AF9CC66';

    protected $baseInfoApi = [
        'siteMoney'  => 'PrePay/v1',
        'mailNo'     => 'mailno/v1',
        'branchList' => 'SiteQuery/v1',
    ];

    protected $requestType = 'get';

    /**
     * @param $waybillNo
     * @param $cmInfo
     * @return string
     */
    public function waybillNumber($waybillNo, $cmInfo)
    {
        $this->api = 'WaybillNumber/v1';

        $data = [
            'WaybillNumber' => $waybillNo,
            'StationCode'   => $cmInfo['branch_code'],
            'UserCode'      => $cmInfo['cm_code'],
            'ak'            => '123456',
        ];

        return $this->request(self::WAYBILL_URL, $data, [], 'Infocode', 'Info', 'Interceptors');
    }

    /**
     * @param $cmInfo
     * @return string
     */
    public function siteMoney($cmInfo)
    {
        $data = [
            'empCode'  => $cmInfo['cm_code'],
            'siteCode' => $cmInfo['branch_code'],
        ];

        return $this->BaseInfo(self::BASE_INFO_URl . $this->getApi(__FUNCTION__), $data);
    }

    /**
     * @param $waybillNo
     * @param $branchCode
     * @return string
     */
    public function mailNo($waybillNo, $branchCode)
    {
        $data = [
            'mailNo'   => $waybillNo,
            'siteCode' => $branchCode,
        ];

        return $this->BaseInfo(self::BASE_INFO_URl . $this->getApi(__FUNCTION__), $data);
    }

    /**
     * @param $type 1表示目的地，2表示网点
     * @return string
     */
    public function branchList($type)
    {
        $data = [
            'type' => $type,
        ];

        $options = [
            'TIMEOUT' => 5,
        ];

        return $this->BaseInfo(self::BASE_INFO_URl . $this->getApi(__FUNCTION__), $data, $options, 'Success', 'ErrMsg', 'Sites');
    }

    /**
     * @param $api
     * @return mixed
     */
    public function getApi($api)
    {
        $this->api = $api;
        return $this->baseInfoApi[$api];
    }

    /**
     * @param $url
     * @param $data
     * @param array $options
     * @param string $status
     * @param string $msg
     * @param string $result
     * @return string
     */
    public function BaseInfo($url, $data, $options = [], $status = 'Success', $msg = 'Message', $result = 'Intercept')
    {
        $time1 = time();

        $sign = [
            'timestamp' => $time1,
            'msgsource' => 'accesstoken',
            'ak'        => base64_encode(md5(date('Y-m-d H:i:s', $time1) . self::BASE_INFO_KEY)),
        ];
        $post = $data + $sign;

        if(empty($options)){
            $options = [
                'TIMEOUT'    => 5,
            ];
        }

        return $this->request($url, $post, $options, $status, $msg, $result);
    }
}